﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OperGui.Models;
using System.Xml;
using System.IO;
using System.Windows.Resources;
using System.Windows;
using System.Xml.Linq;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Data.Linq;
using OperGui.ViewModel;

namespace OperGui
{
    public class DataDispacher
    {
        #region fields
        // private ObservableCollection<Model> sectionViewData;
        static ObservableCollection<Dictionary<string, object>> errors;
        static DataDispacher instance;
        static List<CStructureItem> StructureList;
        // static valuepair _index;
        #endregion
        //dfjdfdf
        protected DataDispacher()
        {
            errors = new ObservableCollection<Dictionary<string, object>>();
            StructureList = new List<CStructureItem>();
            initializedErrorsList();
        }

        //Initial data for testing
        static void initializedErrorsList()
        {
            #region initial errors
            Dictionary<string, object> row = new Dictionary<string, object>();
            row.Add("Location", "GrandParentName");
            row.Add("Section", "ParentName");
            row.Add("CustomSection", "sectionTranslation.GetTranslation(ParentName)");
            row.Add("Status", "Status");
            row.Add("ElementName", "DisplayName");
            row.Add("ElementShortId", "ShortId");
            row.Add("Timestamp", "ErrorTime");
            row.Add("ElementId", "Id");
            row.Add("ProblemKey", "ProblemKey");
            row.Add("SolutionKey", "ProblemKey");
            row.Add("SortId", "Id");
            row.Add("Updated", "true");
            row.Add("RawAreaId", "RawAreaId");
            row.Add("RawZoneId", "Zone3");
            row.Add("RawSectionId", "RawSectionId");
            row.Add("MarkCode", "MarkCode");
            row.Add("ExtendedId", "ExtendedId");
            row.Add("Type", "Type");
            errors.Add(row);
            row = new Dictionary<string, object>();
            row.Add("Location", "GrandParentName1");
            row.Add("Section", "ParentName1");
            row.Add("CustomSection", "sectionTranslation.GetTranslation(ParentName)11");
            row.Add("Status", "Status1");
            row.Add("ElementName", "DisplayName1");
            row.Add("ElementShortId", "ShortId1");
            row.Add("Timestamp", "ErrorTime1");
            row.Add("ElementId", "Id1");
            row.Add("ProblemKey", "ProblemKey1");
            row.Add("SolutionKey", "ProblemKey1");
            row.Add("SortId", "Id1");
            row.Add("Updated", "true");
            row.Add("RawAreaId", "RawAreaId1");
            row.Add("RawZoneId", "Zone1");
            row.Add("RawSectionId", "RawSectionId1");
            row.Add("MarkCode", "MarkCode1");
            row.Add("ExtendedId", "ExtendedId1");
            row.Add("Type", "Type1");
            errors.Add(row);
            row = new Dictionary<string, object>();
            row.Add("Location", "GrandParentName2");
            row.Add("Section", "ParentName2");
            row.Add("CustomSection", "sectionTranslation.GetTranslation(ParentName)2");
            row.Add("Status", "Status2");
            row.Add("ElementName", "DisplayName2");
            row.Add("ElementShortId", "ShortId2");
            row.Add("Timestamp", "ErrorTime2");
            row.Add("ElementId", "Id2");
            row.Add("ProblemKey", "ProblemKey2");
            row.Add("SolutionKey", "ProblemKey2");
            row.Add("SortId", "Id2");
            row.Add("Updated", "true");
            row.Add("RawAreaId", "RawAreaId2");
            row.Add("RawZoneId", "Zone0");
            row.Add("RawSectionId", "RawSectionId2");
            row.Add("MarkCode", "MarkCode2");
            row.Add("ExtendedId", "ExtendedId2");
            row.Add("Type", "Type2");
            errors.Add(row);
            #endregion
            #region initial structure.
            StructureList.Add(new CStructureItem("Zone1", "Zone", "dd"));
            StructureList.Add(new CStructureItem("Zone2", "Zone", "dd"));
            StructureList.Add(new CStructureItem("Zone3", "Zone", "dd"));
            StructureList.Add(new CStructureItem("Zone4", "Zone", "dd"));
            StructureList.Add(new CStructureItem("Zone0", "Zone", "dd"));
            StructureList.Add(new CStructureItem("Section1", "Zone0", "dd"));
            StructureList.Add(new CStructureItem("Section2", "Zone0", "dd"));
            StructureList.Add(new CStructureItem("Section3", "Zone0", "dd"));
            StructureList.Add(new CStructureItem("Section4", "Zone0", "dd"));
            StructureList.Add(new CStructureItem("Section1", "Zone1", "dd"));
            StructureList.Add(new CStructureItem("Section2", "Zone1", "dd"));
            StructureList.Add(new CStructureItem("Section3", "Zone1", "dd"));
            StructureList.Add(new CStructureItem("Section4", "Zone1", "dd"));
            StructureList.Add(new CStructureItem("Section1", "Zone2", "dd"));
            StructureList.Add(new CStructureItem("Section2", "Zone2", "dd"));
            StructureList.Add(new CStructureItem("Section3", "Zone2", "dd"));
            StructureList.Add(new CStructureItem("Section4", "Zone2", "dd"));
            StructureList.Add(new CStructureItem("Section1", "Zone3", "dd"));
            StructureList.Add(new CStructureItem("Section2", "Zone3", "dd"));
            StructureList.Add(new CStructureItem("Section3", "Zone3", "dd"));
            StructureList.Add(new CStructureItem("Section4", "Zone3", "dd"));
            StructureList.Add(new CStructureItem("Section1", "Zone4", "dd"));
            StructureList.Add(new CStructureItem("Section2", "Zone4", "dd"));
            StructureList.Add(new CStructureItem("Section3", "Zone4", "dd"));
            StructureList.Add(new CStructureItem("Section4", "Zone4", "dd"));
            StructureList.Add(new CStructureItem("Element2", "Section1", "dd"));
            StructureList.Add(new CStructureItem("Element1", "Section2", "dd"));
            #endregion
        }

        #region old- part
        /* public List<Model> LoadFileData(string DataFile)
        {
            using (Stream stream = GetResourceStream(DataFile))
            using (XmlReader xmlRdr = new XmlTextReader(stream))
                return (from customerElem in XDocument.Load(xmlRdr).Element("Zone").Elements("section")
                        select new Model((string)customerElem.Attribute("lastName"))).ToList();
        }

        static Stream GetResourceStream(string resourceFile)
        {
            Uri uri = new Uri(resourceFile, UriKind.RelativeOrAbsolute);
            StreamResourceInfo info = Application.GetResourceStream(uri);
            if (info == null || info.Stream == null)
                throw new ApplicationException("Missing resource file: " + resourceFile);
            return info.Stream;
        }

        //public ObservableCollection<Model> sectionData
        //{
        //    set
        //    {
        //        sectionViewData = value;
        //        OnPropertyChanged("sectionData");
        //    }
        //    get { return sectionViewData; }
        //}

        List<Model> LoadSectionData(string SectionData)
        {
            using (Stream stream = GetResourceStream2(SectionData))
            using (XmlReader xmlRdr = new XmlTextReader(stream))
                return (from customerElem in XDocument.Load(xmlRdr).Element("zones").Elements("zone")
                        select new Model((string)customerElem.Attribute("Section"))).ToList();
        }

        static Stream GetResourceStream2(string resourceFile2)
        {
            Uri uri = new Uri(resourceFile2, UriKind.RelativeOrAbsolute);
            StreamResourceInfo info = Application.GetResourceStream(uri);
            if (info == null || info.Stream == null)
                throw new ApplicationException("Missing resource file: " + resourceFile2);
            return info.Stream;
        }
        public ObservableCollection<Dictionary<string, object>> Errors
        {
            get { return errors; }
            set { errors = value; OnPropertyChanged("Errors"); }
        }

        public valuepair Index
        {
            get { return _index; }
            set { _index = value; OnPropertyChanged("Index"); }
        }*/
        #endregion


        //return a static instance of DataDidpacher
        public static DataDispacher Instance
        {
            get
            {
                if (instance == null)
                {
                    DataDispacher.instance = new DataDispacher();
                }
                return instance;
            }
        }



        //Return the ErrorList
        public ObservableCollection<Error> LoadFSCErrors(string idtype, string ParentId)
        {

            if (ParentId != null && idtype != null)
            {
                return new ObservableCollection<Error>((from error in errors
                                                        where (string)error[idtype] == ParentId
                                                        select new Error(error)).ToList());
            }
            return new ObservableCollection<Error>((from error in errors
                                                    select new Error(error)).ToList());
        }

        //For test only
        public void ChangeData()
        {
            errors[2]["Location"] = "Wijzig";
        }

        //Get structure list base on and parentId
        //Give null to type and ParentId if the list are not very applicable.
        public ObservableCollection<CStructureItem> LoadFSCStructureItems(string ParentId)
        {
            if (ParentId != null)
                return new ObservableCollection<CStructureItem>((from item in StructureList
                                                                 where item.ParentId == ParentId
                                                                 select item).ToList());
            else return null;

        }
    }

    //Object used to retrive the correct info from errors list.
    //public class valuepair
    //{
    //    public valuepair(string _type, string _id)
    //    {
    //        idtype = _type;
    //        id = _id;
    //    }
    //    public string idtype;
    //    public string id;
    //}
}